import FileSaver from 'file-saver'
import XLSX from 'xlsx'
import { v4 as uuidv4 } from 'uuid'
export const exportTableAsXLSX = function(_targetId) {
  // 根据table生成Book工作簿
  const wb = XLSX.utils.table_to_book(document.getElementById(_targetId))
  // 将Book工作簿作为输出
  const wbout = XLSX.write(wb, {
    bookType: 'xlsx',
    bookSST: true,
    type: 'array'
  })
  // 尝试将当前table内容保存为excel文件
  try {
    FileSaver.saveAs(
      // 被导出的blob二进制对象
      new Blob([wbout], { type: 'application/octet-stream' }),
      // 导出文件的名称+后缀名
      uuidv4() + '.xlsx'
    )
  } catch (e) {
    if (typeof console !== 'undefined') console.log(e, wbout)
  }
}
